﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OntologyFormDesigner.GuiComponent;

namespace OntologyFormDesigner
{
    public partial class WizardForm : Form
    {
        DialogResult result = DialogResult.Cancel;

        public WizardForm()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            btnBack.Enabled = false;
            btnNext.Enabled = false;
            btnFinish.Enabled = false;
            btnCancel.Enabled = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            result = DialogResult.Cancel;
            this.Close();

        }

        public void Next()
        {
            btnNext.Enabled = true;
        }

        public void DisableNext() {
            btnNext.Enabled = false;
        }

        public void Back()
        {
            btnBack.Enabled = true;
        }

        public void Finish() {
            btnFinish.Enabled = true;
            result = DialogResult.OK;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                WizardPanel wP = (WizardPanel)panel2.Controls[0];
                int index = pages.IndexOf(wP);
                wP.Next();
                panel2.Controls.Clear();
                WizardPanel wP_1 = pages[index + 1];
                wP_1.createControl();
                wP_1.Dock = DockStyle.Top;
                panel2.Controls.Add(pages[index + 1]);
                wP = (WizardPanel)panel2.Controls[0];
                btnBack.Enabled = true;

                if (wP.NextPage == null)
                {
                    btnNext.Enabled = false;
                    btnFinish.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                btnNext.Enabled = false;
                btnFinish.Enabled = true;
                btnBack.Enabled = true;
                MessageBox.Show("E' stata sollevata una eccezione: " + ex.Message);
            }

        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            result = DialogResult.OK;
            this.Close();
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            if (pages.Count == 1)
            {
                panel2.Controls.Clear();
                panel2.Controls.Add(pages[0]);
                btnFinish.Enabled = false;
                btnBack.Enabled = false;
            }
            else
            {

                try
                {
                    WizardPanel wP = (WizardPanel)panel2.Controls[0];
                    int index = pages.IndexOf(wP);

                    panel2.Controls.Clear();
                    panel2.Controls.Add(pages[index - 1]);

                    wP = (WizardPanel)panel2.Controls[0];
                    index = pages.IndexOf(wP);

                    if (index == 0) 
                        btnBack.Enabled = false;

                    btnNext.Enabled = true;

                }
                catch (Exception)
                {
                    //btnNext.Enabled = false;
                    //btnFinish.Enabled = true;
                    //btnBack.Enabled = true;

                    panel2.Controls.Clear();
                    panel2.Controls.Add(pages[0]);
                }
            }
        }

        private void WizardForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.DialogResult = result;
        }
    }
}
